<?php
/**
 * 链接管理 - JBlog
 *
 * @copyright (c) 2008-2012 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: Link.php 30 2012-10-15 06:08:28Z lisijie86@gmail.com $
 */
class LinkController extends BaseController {
	
	protected function _init_() {
		$this->addTab(__('链接管理'), 'link.index');
		$this->addTab(__('添加链接'), 'link.add');
		$this->addTab(__('链接分类'), 'link.category');
	}
	
	public function indexAction() {
		$cid = intval($this->input->cid);
		$page = max(1, intval($this->input->page));
		$keyword = String::safeStr($this->input->keyword);
		$pagesize = 20;
		
		$wheresql = '1';
	
		if (!empty($keyword)) $wheresql .= " AND (l.`name` LIKE '%$keyword%' OR l.url LIKE '%$keyword%')";
		if ($cid > 0) $wheresql .= " AND l.cateid = $cid";
	
		$count = $this->db->count($this->db->getTable('link')." AS l", "$wheresql");
		$list  = array();
		if ($count > 0) {
			$sql = "SELECT l.*,c.catename FROM ".$this->db->getTable('link')." l LEFT JOIN ".$this->db->getTable('cate')." c ON l.cateid = c.id 
			WHERE $wheresql ORDER BY l.cateid ASC,l.orderid ASC";
			$list = $this->db->getList($sql, $page, $pagesize);
		}
		
		$pager = new Pager($page, $count, compact('cid','keyword'));
		$this->assign('keyword', $keyword);
		$this->assign('pagebar', $pager->makeHtml());
		$this->display();
	}
	
	public function addAction() {
		if ( !$tdb->count('cate', "`type`='link'") ) {
			prompt(__('当前还没有链接分类，请先添加一个分类。'), '?mod=link&ac=cate');
		}
		$result = array('status'=>1);
	}
	
	public function editAction() {
		
	}
	
	/**
	 * 更改状态
	 */
	public function upstatAction() {
		$val = $_GET['val']?1:0;
		$db->update('link', array('status'=>$val), array('id'=>intval($_GET['id'])));
		recache('link');
		do_action('link_upstat');
		redirect($_USER['refer']);
	}
	
	/**
	 * 删除链接
	 */
	public function deleteAction() {
		$idarr = $upcate = array();
		foreach ( (array)$_POST['id'] as $id ) {
			if ( $id && is_numeric($id) ) {
				$idarr[] = $id;
			}
		}
		if ( $idarr ) {
			$query = $db->query("SELECT cateid FROM ".tname('link')." WHERE id IN (".simplode($idarr).")");
			while ( $row = $db->fetch_array($query) ) {
				$upcate[$row['cateid']]++;
			}
			$db->delete('link', array('id'=>$idarr));
			foreach ( $upcate as $key => $val ) {
				$db->query("UPDATE ".tname('cate')." SET `count` = `count` - '$val' WHERE id = '$key'");
			}
			recache('link,cate');
			do_action('link_del');
		}
		redirect($_USER['refer']);		
	}
	
	/**
	 * 移到到其他分类
	 */
	public function moveAction() {
		$idarr = $upcate = array();
		foreach ( (array)$_POST['id'] as $id ) {
			if ( $id && is_numeric($id) ) {
				$idarr[] = $id;
			}
		}
		$cid = intval($_POST['cateid']);
		if ( $db->result("SELECT id FROM ".tname('cate')." WHERE id = '$cid' AND `type` = 'link'") ) {
			$upcate[] = $cid;
			$query = $db->query("SELECT cateid FROM ".tname('link')." WHERE id IN (".simplode($idarr).")");
			while ( $row = $db->fetch_array($query) ) {
				$upcate[]=$row['cateid'];
			}
			$db->update('link', array('cateid'=>$cid), array('id'=>$idarr));
			foreach ( $upcate as $val ) {
				$count = $db->count('link', "cateid = '$val'");
				$db->update('cate', array('count'=>$count),array('id'=>$val));
			}
			recache('link,cate');
			do_action('link_move');
		}
		redirect($_USER['refer']);
	}
	
	/**
	 * 保存链接
	 */
	private function saveLink() {
		initGP(array('id','cateid','name','url','logo','description','status'));
		if ( empty($name) ) prompt(__('网站名称不能为空。'));
		if ( empty($url) ) prompt(__('链接地址不能为空。'));
		$link = array(
			'cateid' => intval($cateid),
			'name' => check_str($name),
			'url' => check_str($url),
			'logo' => check_str($logo),
			'description' => check_str($description),
			'status' => intval($status)
		);
		if ( $id && is_numeric($id) ) {
			check_perm_prompt('link','eidt');
			$oldcid = $db->result("SELECT cateid FROM ".tname('link')." WHERE id = '$id'");
			$db->update('link', $link, array('id'=>$id));
			if ( $oldcid != $link['cateid'] ) {
				$count = $db->count('link', "cateid={$oldcid}");
				$db->update('cate', array('count'=>$count), array('id'=>$oldcid));
				$count = $db->count('link', "cateid={$link[cateid]}");
				$db->update('cate', array('count'=>$count), array('id'=>$link['cateid']));
			}
		} else {
			check_perm_prompt('link','add');
			$db->insert('link', $link);
			$db->query("UPDATE ".tname('cate')." SET `count` = `count` + 1 WHERE id = '$cateid'");
		}
		recache('link');
		do_action('link_save');
		redirect('?mod=link');
	}
}
